package de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.data.MultinomialOnlyCorpus;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.data.Word;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.data.WordCorpus;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CSVTokenizer;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CreateFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CreateGraph;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.FeatureType;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.CaseFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.PunctuationFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.SuffixFeatures;
/**
 * 
 * @author Christoph Teichmann
 *
 */
public class TestMultinomialOnlyCorpus
{
	/**
	 * 
	 */
	private WordCorpus wc;
	@Before
	public void setUp() throws Exception
	{
		wc = new MultinomialOnlyCorpus();
		FeatureType[] ft = new FeatureType[] {new SuffixFeatures(2),new PunctuationFeatures(),
				new CaseFeatures()};
		makeFeaturePart(ft);
	}
	/**
	 * 
	 * @param ft
	 * @throws IOException
	 */
	private void makeFeaturePart(FeatureType[] ft) throws IOException
	{
		CreateFeatures cf = new CreateFeatures(ft);
		StringReader sr = new StringReader(TestStrings.ALIGNMENTS);
		BufferedReader br = new BufferedReader(sr);
		cf.addFeatures(br, new CSVTokenizer(false));
		br.close();
		addFeatureLexicon(ft,cf);
		addWords(cf,ft);
	}
	/**
	 * 
	 * @param cf
	 * @param ft 
	 * @throws IOException
	 */
	private void addWords(CreateFeatures cf, FeatureType[] ft) throws IOException
	{
		StringReader sr;
		BufferedReader br;
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		cf.writeOutFeatures(bw);
		bw.close();
		sr = new StringReader(sw.toString());
		br = new BufferedReader(sr);
		CreateGraph cg = new CreateGraph(true, false, ft);
		StringReader sb = new StringReader(TestStrings.ALIGNMENTS);
		BufferedReader bb = new BufferedReader(sb);
		cg.addAlignments(bb);
		bb.close();
		StringWriter sc = new StringWriter();
		BufferedWriter bc = new BufferedWriter(sc);
		cg.writeOutEdges(bc);
		bc.close();
		sb = new StringReader(sc.toString());
		bb = new BufferedReader(sb);
		wc.addWordsAndConnections(br, bb);
		br.close();
	}
	/**
	 * 
	 * @param ft
	 * @param cf
	 * @throws IOException
	 */
	private void addFeatureLexicon(FeatureType[] ft, CreateFeatures cf) throws IOException
	{
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		cf.writeOutFeatureLexicon(bw);
		bw.close();
		StringReader sr = new StringReader(sw.toString());
		BufferedReader br = new BufferedReader(sr);
		wc.addFeatures(br);
	}
	@Test
	public void testIterator()
	{
		Iterator<Word> it = wc.iterator();
		Set<String> s = new HashSet<>();
		while(it.hasNext())
		{
			Word w = it.next();
			StringBuilder sb = new StringBuilder();
			sb.append(wc.getName(w.getIdentity()));
			sb.append("|");
			sb.append(wc.getLanguageName(w.getLanguage()));
			sb.append(" ");
			sb.append(w.getAmount());
			for(int i=0;i<w.getNumberOfFeatures();++i)
			{
				sb.append(" ");
				sb.append(wc.getName(w.getFeatureName(i)));
				sb.append(" ");
				sb.append(wc.getName(wc.getFeatureGroup(w.getFeatureName(i))));
				sb.append(" ");
				sb.append((w.getFeatureValue(i)));
			}
			sb.append("\n");
			for(int i=0;i<w.getNumberOfConnections();++i)
			{
				sb.append(" ");
				Word v = w.getConnectedWord(i);
				sb.append(wc.getName(v.getIdentity()));
				sb.append("|");
				sb.append(wc.getLanguageName(v.getLanguage()));
				sb.append(" ");
				sb.append(w.getConnectionWeight(i));
				sb.append(" ");
				sb.append(wc.getName(w.getConnectionLabel(i)));
			}
			s.add(sb.toString());
		}
		assertTrue(s.contains("har|DEUTSCH 2.0 LOWERCASE.CASEFEATURE CASEFEATURE 1.0 NOPUNCT.PUNCTUATIONFEATURES PUNCTUATIONFEATURES 2.0 INITIALUPPERCASE.CASEFEATURE CASEFEATURE 1.0 ar.SUFFIXFEATURES SUFFIXFEATURES 2.0\n"
					+" har|DEUTSCH 1.0 FOLLOWING kk|ENGLISH 2.0 TRANSLATE"));
	}
	@Test
	public void testMIterator() throws InterruptedException
	{
		Set<String> s = new HashSet<>();
		for(int i=0;i<5;++i)
		{
			Iterator<Word> it = wc.mIterator();
			Word w = it.next();
			StringBuilder sb = new StringBuilder();
			sb.append(wc.getName(w.getIdentity()));
			sb.append("|");
			sb.append(wc.getLanguageName(w.getLanguage()));
			s.add(sb.toString());
			for(int h=0;h<100000000;++h)
			for(int k=0;k<100000000;++k)
			{}
		}
		assertTrue(s.size() > 1);
	}
}